/* 
 * ResourceMgr.cs
 * Part of Gamadus Engine. Visit www.gamadus.de.vu!
 * 
 * [DONE] Commenting finished.
 */
 
using System;
using System.Collections.Generic;
using System.IO;

namespace Gamadus.Engine
{
	public enum ResourceTypes
	{
		Object,
		Room,
		Instance
	}
	/// <summary>
	/// The resource manager. Used to load and manage the resources
	/// </summary>
	public partial class ResourceMgr : SingletonManager<ResourceMgr>
	{
		/// <summary>
    	/// Holds all resources with it's ID as key
    	/// </summary>
    	Dictionary<ResourceTypes, Dictionary<int, Resource>> resourceTable = new Dictionary<ResourceTypes, Dictionary<int, Resource>>();
		
		/// <summary>
		/// Inits the Manager. Call before starting main loop.
		/// </summary>
		public void Init()
		{
			foreach(ResourceTypes type in Enum.GetValues(typeof(ResourceTypes)))
			{
				resourceTable[type] = new Dictionary<int, Resource>();
			}
			Resources.Instance.Init();
			LoadSettings();
			LoadResourceFile();
		}
		/// <summary>
		/// Creates a new room
		/// </summary>
		/// <param name="name">The name of the room</param>
		/// <returns>Returns the instance of Room</returns>
		public int CreateRoom(int id)
		{
			resourceTable[ResourceTypes.Room][id] = RoomMgr.Instance.CreateRoom(id);
			return id;
		}
		
		public Room GetRoom(int id)
		{
			Resource res;
			if(resourceTable[ResourceTypes.Room].ContainsKey(id) && (res = resourceTable[ResourceTypes.Room][id]) is Room)
				return (Room)res;
			
			throw new ArgumentException("Resource is not a room");
		}
		
		public int CreateObject(int id)
		{
			resourceTable[ResourceTypes.Object][id] = new Object(id);
			return id;
			
		}
		
		public Gamadus.Engine.Object GetObject(int id)
		{
			Resource res;
			if(resourceTable[ResourceTypes.Object].ContainsKey(id) && (res = resourceTable[ResourceTypes.Object][id]) is Gamadus.Engine.Object)
				return (Gamadus.Engine.Object)res;
			
			throw new ArgumentException("Resource is not a room");
		}
		
		/// <summary>
		/// Creates an instance of an object in a specified room
		/// </summary>
		/// <param name="type">The object</param>
		/// <param name="position">The position the instance will be created at</param>
		/// <param name="lev">The room the instance will be created in</param>
		/// <returns></returns>
		public int CreateInstance(Gamadus.Engine.Object obj, double x, double y, Room lev)
		{
			Console.WriteLine("Creating instance of object with ID "+obj.ID);
			if(lev == null)
				throw new ArgumentNullException("Given Room is not initialised");
			
			//TODO: get next id instead of 0
			Instance inst = new Instance(0, x, y, lev);
			resourceTable[ResourceTypes.Instance].Add(0,inst);
			lev.Instances.Add(inst);
			inst.CreateEvent += ResourceMgr.Instance.HandleTest;
			inst.Create();
			return 0;
		}
		
		/// <summary>
		/// Creates an instance of an object in the current room
		/// </summary>
		/// <param name="type">The object</param>
		/// <param name="position">The position the instance will be created at</param>
		/// <returns></returns>
		public int CreateInstance(Object obj, double x, double y)
		{
			return CreateInstance(obj, x, y, RoomMgr.Instance.CurrentRoom);
		}
		
		public int CreateInstance(int objectID, double x, double y, Room room)
		{
			return CreateInstance(GetObject(objectID), x, y, room);
		}
		
		public int CreateInstance(int objectID, double x, double y)
		{
			return CreateInstance(GetObject(objectID), x, y, RoomMgr.Instance.CurrentRoom);
		}
		
		void LoadResourceFile()
		{
//			string appDir = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
//			string tempDir = Path.Combine(appDir, "temp");
//			int tempInd = 0;
//			while(Directory.Exists(tempDir))
//			{
//				tempDir = Path.Combine(appDir, "temp"+(tempInd++));
//			}
//			if(!Directory.Exists(tempDir))
//				Directory.CreateDirectory(tempDir);
//			tempDir+=Path.DirectorySeparatorChar;
//			Console.WriteLine("Temp directory will be "+tempDir);
//			try
//			{
//				System.Resources.ResourceManager rm = new System.Resources.ResourceManager("includes", System.Reflection.Assembly.GetExecutingAssembly());
//				foreach(object o in ResourceTable.Values)
//				{
//					if(o is Font)
//					{
//						Font font = (Font)o;
//						byte[] bytes = (byte[])rm.GetObject(font.file);
//						//TODO!
//						font.font = null;//new SdlDotNet.Graphics.Font(bytes, font.size);
//					}
//					if(o is Sprite)
//					{
//						Sprite spr = (Sprite)o;
//						//Load sprite
//					}
//				}
//			}
//			finally
//			{
//				//clear and delete temp directory
//				foreach(string file  in Directory.GetFiles(tempDir))
//				{
//					File.Delete(file);
//				}
//				Directory.Delete(tempDir);
//			}
		}
	}
}
